<template>
  <el-card>
    <!-- 查询表单 -->
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      size="mini"
    >
      <el-form-item label="订单号">
        <el-input v-model="formInline.orderNo" placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="收货人">
        <el-input
          v-model="formInline.consignee"
          placeholder="收货人"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formInline.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="formInline.orderState" placeholder="订单状态">
          <el-option label="已受理" value="已受理"></el-option>
          <el-option label="派送中" value="派送中"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择时间">
        <el-date-picker
          v-model="formInline.date"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="orderNo" label="订单号" width="150">
      </el-table-column>
      <el-table-column label="下单时间" width="180">
        <template v-slot="{ row }">
          <p>{{ row.orderTime | formateDate }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="120">
      </el-table-column>
      <el-table-column prop="consignee" label="收货人" width="120">
      </el-table-column>
      <el-table-column prop="deliverAddress" label="配送地址" width="300">
      </el-table-column>
      <el-table-column label="送达时间" width="180">
        <template v-slot="{ row }">
          <p>{{ row.deliveryTime | formateDate }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="remarks" label="用户备注" width="120">
      </el-table-column>
      <el-table-column prop="orderAmount" label="订单金额" width="120">
      </el-table-column>
      <el-table-column prop="orderState" label="订单状态" width="120">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template v-slot="{ row }">
          <el-button @click="handleClick(row)" type="text" size="small"
            >查看</el-button
          >
          <el-button type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </el-card>
</template>

<script>
import { orderList } from '../../api/order.js'
import moment from 'moment'
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      total: 1,
      tableData: [],
      // 查询表单
      formInline: {
        orderNo: '',
        consignee: '',
        phone: '',
        orderState: '',
        date: []
      }
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    handleClick(row) {
      // console.log(row)
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getOrderList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getOrderList()
    },
    async getOrderList() {
      const { data, total } = await orderList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        orderNo: this.formInline.orderNo,
        consignee: this.formInline.consignee,
        phone: this.formInline.phone,
        orderState: this.formInline.orderState,
        date: JSON.stringify(this.formInline.phone)
      })
      // console.log(data)
      this.tableData = data
      this.total = total
    },
    onSubmit() {
      // 查询的时候,只能从当前页往后面查,所以要设置起始页码为1
      this.currentPage = 1
      this.getOrderList()
    }
  },
  filters: {
    formateDate(str) {
      return moment(str).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  margin-bottom: 20px;
}
.el-pagination {
  margin-top: 30px;
}
</style>
